//
//  JYJSeachProgramCollectionViewCellTwo.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/9.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJSeachProgramCollectionViewCellTwo.h"
#import "BaseTableView.h"
#import "JYJTAMacro.h"
#import "JYJSeachContentSegmentTwoTableViewCell.h"
#import "JYJSeachProgramTableViewCell.h"
#import "JYJNetWorkParser.h"
#import "JYJSeachProgramModelOne.h"
#import "JYJSeachProgramModelTwo.h"
#import "Masonry.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "MJRefresh.h"
@interface JYJSeachProgramCollectionViewCellTwo ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray<JYJSeachProgramModelOne *> *marrOfModel;
@property (nonatomic, strong) NSMutableArray<JYJSeachProgramModelTwo *> *marrOfSeachModel;
@property (nonatomic, assign) NSInteger loadCount;
@property (nonatomic, strong) NSDictionary *dic;
@property (nonatomic, strong) NSDictionary *dicTwo;
@end
@implementation JYJSeachProgramCollectionViewCellTwo
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        self.loadCount = 1;
    }
    return self;
}
- (void)createTableView {
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
-(void)setInPutWord:(NSString *)inPutWord {
    _inPutWord = inPutWord;
    //json=%7B%22item_id%22%3A%220%22%2C%22keyword%22%3A%22%E4%B8%8D%22%2C%22orderBy%22%3A0%2C%22recomm%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%7D
    //json=%7B%22item_id%22%3A%220%22%2C%22keyword%22%3A%22%E4%B8%8D%22%2C%22orderBy%22%3A0%2C%22recomm%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%7D
    NSString *str1 = @"json=%7B%22item_id%22%3A%220%22%2C%22keyword%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%@",inPutWord];
    NSString *str3 = @"%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse: JYJJSON andSuccessBlock:^(id result) {
        
        NSNumber *num = result[@"paginated"][@"count"];
        self.dic = result[@"paginated"];
        if (![num isEqualToNumber:@0]) {
            self.marrOfModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelOne *model = [JYJSeachProgramModelOne modelWithDic:dic];
                [self.marrOfModel addObject:model];
            }
            [self.tableView reloadData];
            
        }else {
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
            image.frame = CGRectMake(0, 0, 150,150);
            image.center = self.contentView.center;
            [self.contentView addSubview:image];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"未搜到相关内容";
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:25];
            [self.contentView addSubview:label];
            label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(100);
                make.right.equalTo(self.contentView.mas_right).offset(-100);
                make.bottom.equalTo(image.mas_top).with.offset(20);
                make.height.mas_equalTo(80);
            }];
            
        }

    } andFailureBlock:^(NSError *error) {
        
    }];
    NSString *str4 = @"%22%2C%22orderBy%22%3A9%2C%22recomm%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%7D";
    NSString *str = [NSString stringWithFormat:@"%@",inPutWord];
    str = [[str1 stringByAppendingString:str]stringByAppendingString:str4];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:str andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
         NSNumber *num = result[@"paginated"][@"count"];
        self.dicTwo = result[@"paginated"];
        if (![num isEqualToNumber:@0]) {
            self.marrOfSeachModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelTwo *model = [JYJSeachProgramModelTwo modelWithDic:dic];
                [self.marrOfSeachModel addObject:model];
            }
            
            [self.tableView reloadData];
        }else {
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
            image.frame = CGRectMake(0, 0, 150,150);
            image.center = self.contentView.center;
            [self.contentView addSubview:image];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"未搜到相关内容";
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:25];
            [self.contentView addSubview:label];
            label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(100);
                make.right.equalTo(self.contentView.mas_right).offset(-100);
                make.bottom.equalTo(image.mas_top).with.offset(20);
                make.height.mas_equalTo(80);
            }];
            
        }
        

    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
    [self.tableView registerClass:[JYJSeachProgramTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 下拉刷新动画
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        NSString *name = [NSString stringWithFormat:@"list_load_%d",i];
        UIImage *image = [UIImage imageNamed:name];
        [marr addObject:image];
    }
    //    /** 普通闲置状态 */
    //    MJRefreshStateIdle = 1,
    //    /** 松开就可以进行刷新的状态 */
    //    MJRefreshStatePulling,
    //    /** 正在刷新中的状态 */
    //    MJRefreshStateRefreshing,
    
    [header setImages:marr forState:MJRefreshStateIdle];
    [header setImages:marr forState:MJRefreshStatePulling];
    [header setImages:marr forState: MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
   self.tableView.mj_footer = [MJRefreshAutoNormalFooter  footerWithRefreshingTarget:self refreshingAction:@selector(handleMore)];

}
#pragma mark----------------刷新
- (void)loadNewData {
    NSString *str1 = @"json=%7B%22item_id%22%3A%220%22%2C%22keyword%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%@",_inPutWord];
    NSString *str3 = @"%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string] stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse: JYJJSON andSuccessBlock:^(id result) {
        
        NSNumber *num = result[@"paginated"][@"count"];
        if (![num isEqualToNumber:@0]) {
            self.marrOfModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelOne *model = [JYJSeachProgramModelOne modelWithDic:dic];
                [self.marrOfModel addObject:model];
            }
            [self.tableView reloadData];
            
        }else {
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
            image.frame = CGRectMake(0, 0, 150,150);
            image.center = self.contentView.center;
            [self.contentView addSubview:image];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"未搜到相关内容";
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:25];
            [self.contentView addSubview:label];
            label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(100);
                make.right.equalTo(self.contentView.mas_right).offset(-100);
                make.bottom.equalTo(image.mas_top).with.offset(20);
                make.height.mas_equalTo(80);
            }];
            
        }
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self.tableView.mj_header endRefreshing];
    NSString *str4 = @"%22%2C%22orderBy%22%3A9%2C%22recomm%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%7D";
    NSString *str = [NSString stringWithFormat:@"%@",_inPutWord];
    str = [[str1 stringByAppendingString:str]stringByAppendingString:str4];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:str andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        NSNumber *num = result[@"paginated"][@"count"];
        if (![num isEqualToNumber:@0]) {
            self.marrOfSeachModel = [NSMutableArray array];
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelTwo *model = [JYJSeachProgramModelTwo modelWithDic:dic];
                [self.marrOfSeachModel addObject:model];
            }
            
            [self.tableView reloadData];
        }else {
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
            image.frame = CGRectMake(0, 0, 150,150);
            image.center = self.contentView.center;
            [self.contentView addSubview:image];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"未搜到相关内容";
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:25];
            [self.contentView addSubview:label];
            label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(100);
                make.right.equalTo(self.contentView.mas_right).offset(-100);
                make.bottom.equalTo(image.mas_top).with.offset(20);
                make.height.mas_equalTo(80);
            }];
            
        }
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self.tableView.mj_header endRefreshing];
}
#pragma mark-----------------加载
- (void)handleMore {
        self.loadCount += 1;
        NSString *str1 = @"json=%7B%22item_id%22%3A%220%22%2C%22keyword%22%3A%22";
        NSString *string = [NSString stringWithFormat:@"%@",_inPutWord];
        NSString *str3 = @"%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
        NSString *str4 = [NSString stringWithFormat:@"%ld",self.loadCount];
        NSString *str5 = @"%7D%7D";
        string = [[[[str1 stringByAppendingString:string] stringByAppendingString:str3]stringByAppendingString:str4] stringByAppendingString:str5];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse: JYJJSON andSuccessBlock:^(id result){   
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelOne *model = [JYJSeachProgramModelOne modelWithDic:dic];
                [self.marrOfModel addObject:model];
            }
            [self.tableView reloadData];
            
            
        } andFailureBlock:^(NSError *error) {
            
        }];
        [self.tableView.mj_footer endRefreshing];
    
        NSString *str = [NSString stringWithFormat:@"%@",_inPutWord];
        NSString *string4 = @"%22%2C%22orderBy%22%3A9%2C%22recomm%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A";
        NSString *string2 = [NSString stringWithFormat:@"%ld",self.loadCount];
        NSString *string5 = @"%7D%7D";
        
        str = [[[[str1 stringByAppendingString:str]stringByAppendingString:string4] stringByAppendingString:string2] stringByAppendingString:string5];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:str andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJSeachProgramModelTwo *model = [JYJSeachProgramModelTwo modelWithDic:dic];
                [self.marrOfSeachModel addObject:model];
            }
            
            [self.tableView reloadData];
            
        } andFailureBlock:^(NSError *error) {
            
        }];
        [self.tableView.mj_footer endRefreshing];


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.index == 0) {
        return self.marrOfModel.count;
        
    }else {
        return self.marrOfSeachModel.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJSeachProgramTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    if (self.index == 0) {
        JYJSeachProgramModelOne *model = self.marrOfModel[indexPath.row];
        [cell setSeachProgramModelOne:model];
        cell.strOfColor = self.inPutWord;
        
    }else {
        JYJSeachProgramModelTwo *model = self.marrOfSeachModel[indexPath.row];
        [cell setModelTwo:model];
        cell.strOfColorTwo = self.inPutWord;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
    if (self.index == 0) {
        JYJSeachProgramModelOne *model = self.marrOfModel[indexPath.row];
        details.seachProgramModelOne = model;
    }else {
        JYJSeachProgramModelTwo *model = self.marrOfSeachModel[indexPath.row];
        details.seachProgramModelTwo = model;
    }

    self.handleAction(details);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
